package com.bdqn.crm.utils;

import java.util.List;

/**
 * 分页工具类
 *
 * @author chengxukang
 */
public class Page<T> {

    public Page() {
    }

    public Page(Integer number, Integer pageSize) {
        this.number = number;
        this.pageSize = pageSize;
    }

    // 页大小，每页显示多少条数据
    private Integer pageSize = 10;
    // 当前页码，当前所查看的是第几页
    private Integer number = 1;

    // 总页数
    private Integer totalPages = 0;

    // 数据库表中总数据量
    private Integer totalElements; // select count(*) from tab

    // 分页的数据
    private List<T> content;

    //	标记数据库里数据的数量是否为0
    private int sign;

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

    public Integer getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(Integer totalPages) {
        this.totalPages = totalPages;
    }

    public Integer getTotalElements() {
        return totalElements;
    }

    public void setTotalElements(Integer totalElements) {
        if (totalElements > 0) {
            this.totalElements = totalElements;
            // 计算总页数
            // 三元表达式：boolean ? 为true时执行 : 为false时执行;
            this.totalPages = (totalElements % pageSize == 0) ? (totalElements / pageSize) : (totalElements / pageSize + 1);
        } else if (totalElements == 0) {
            //说明数据库一条数据都没有 将页码和总页面，还有数据 全部设置为0 (大概为恢复默认)
            sign = -1;
            this.totalElements = totalElements;
            this.number = 0;
            this.totalPages = 0;
        }
    }

    public int getSign() {
        return sign;
    }

    public void setSign(int sign) {
        this.sign = sign;
    }

    public List<T> getContent() {
        return content;
    }

    public void setContent(List<T> content) {
        this.content = content;
    }
}
